package com.meshnetics.gb.type;

import java.nio.ByteBuffer;

/**
 * Data element supporting meta information (type) and able to serialize itself.
 *
 * @author AKovylin
 */
public interface DataValue {
    /**
     * Returns type of this value.
     *
     * @return type of this value
     */
    DataType getType();

    /**
     * Returns length in bytes of this value. Method {@link #bytesValue()} should return array of same size.
     * Method {@link #serialize(java.nio.ByteBuffer)} should write exactly this amount of bytes to ByteBuffer.
     *
     * @return length in bytes of this value
     */
    int getDataSize();

    /**
     * Converts value to byte array. Bytes should be in Big Endian order.
     *
     * @return byte array representation of this value
     */
    byte[] bytesValue();

    /**
     * Converts value to string.
     *
     * @return string representation of this value
     */
    String stringValue();

    /**
     * Converts value to corresponding Java object.
     *
     * @return Java object representation of this value
     */
    Object toObject();

    /**
     * Writes value to ByteBuffer. In most cases it should be operation similar in result to execution of
     * <code>bb.put(bytesValue());</code>.
     *
     * @param bb ByteBuffer where value mustbe stored
     * @throws NullPointerException thrown if bb is null
     */
    void serialize(ByteBuffer bb);
}
